﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using BawangMemo.BackendModels;
using BawangMemo.BawangMemoView.View;

namespace BawangMemo.BawangMemoView
{
    /// <summary>
    /// IntroWindow.xaml 的交互逻辑
    /// </summary>
    public partial class IntroWindow : Window
    {
        public IntroWindow()
        {
            InitializeComponent();
        }

        #region ButtonEvents

        private void continueButton_Click(object sender, RoutedEventArgs e)
        {
            /*
            string courseName = "霸王";
            if (!CourseRepository.hasCourseName(courseName))
            {
                CourseRepository.CreateCourse(courseName, "AppData/Book/GRE红宝书-丰富版.xml",
                    WordOrder.Sequential, 50);
            }
             */

            //CourseWrapper cw = CourseRepository.LoadCourse(courseName);
            CourseWrapper cw = CourseRepository.LoadLastCourse();

            if (cw == null)
            {
                MessageBox.Show("没有可载入的课程!");
                return;
            }

            //new MainWindow().Show();
            new MainWindow(cw).Show();
            App.IsShuttingDown = false;
            this.Close();
            App.IsShuttingDown = true;
        }

        private void newButton_Click(object sender, RoutedEventArgs e)
        {
            this.grid2.Children.Clear();
            this.grid2.Children.Add(new NewCourseView(this));
        }

        public void ShowCourseInfoView()
        {
            this.grid2.Children.Clear();

            StackPanel panel = new StackPanel();
            this.grid2.Children.Add(panel);

            Dictionary<string, double> allProgress = CourseRepository.GetAllProgress();
            foreach (KeyValuePair<string, double> kvp in allProgress)
            {
                CourseInfoView infoView = new CourseInfoView(kvp.Key, kvp.Value);
                infoView.CourseEventHandler += new RoutedEventHandler(infoView_CourseEventHandler);
                panel.Children.Add(infoView);
            }
        }

        private void loadButton_Click(object sender, RoutedEventArgs e)
        {
            this.grid2.Children.Clear();

            StackPanel panel = new StackPanel();
            this.grid2.Children.Add(panel);

            Dictionary<string, double> allProgress = CourseRepository.GetAllProgress();
            foreach (KeyValuePair<string, double> kvp in allProgress)
            {
                CourseInfoView infoView = new CourseInfoView(kvp.Key, kvp.Value);
                infoView.CourseEventHandler += new RoutedEventHandler(infoView_CourseEventHandler);
                panel.Children.Add(infoView);
            }
        }

        void infoView_CourseEventHandler(object sender, RoutedEventArgs e)
        {
            CourseEventArgs args = (CourseEventArgs)e;

            if (args.IsLoad)
            {
                CourseWrapper cw = CourseRepository.LoadCourse(args.CourseName);
                new MainWindow(cw).Show();
                App.IsShuttingDown = false;
                this.Close();
                App.IsShuttingDown = true;
            }
            else
            {
                //wintokk: Delete
                var r = MessageBox.Show(this, "确定要删除课程吗？（不可恢复!）", "删除课程",
                    MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No);
                if (r == MessageBoxResult.Yes)
                {
                    CourseRepository.DeleteCourse(args.CourseName);
                    loadButton_Click(sender, e);
                }
            }
        }

        private void aboutButton_Click(object sender, RoutedEventArgs e)
        {
            this.grid2.Children.Clear();
            this.grid2.Children.Add((Image)FindResource("credit"));
        }

        private void quitButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
            App.Current.Shutdown();
        }

        #endregion

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (App.IsShuttingDown)
            {
                App.Current.Shutdown();
            }
        }

    }
}
